﻿
#include "FileOperation.h"

#if defined(DEBUG_IN_PC)
#include <windows.h>
#include <string.h>
#include <stdio.h>
#else

#include <string.h>
#include "aworks.h"
#include "aw_delay.h"
#include "fs/aw_mount.h"
#include "fs/aw_blk_dev.h"
#include "aw_vdebug.h"
#include "io/aw_fcntl.h"
#include "io/aw_unistd.h"
#include "io/aw_dirent.h"
#include "io/sys/aw_stat.h"

#endif


#include "GUI.h"






#if defined(DEBUG_IN_PC)
static uint8_t pPath[200], picPath[200];

uint8_t DiskInit(void)
{
	uint8_t *pPos;

	GetCurrentDirectory(200, pPath);

	pPos = strrchr(pPath, '\\');
	pPos++;
	*pPos = 0;

	strcat(pPath, "GUI\\GuiPictures\\");
}


void *GetPictureData(const uint8_t *sFilename, uint32_t *pSize, GUI_HMEM *pMen)
{
	HANDLE hFile;
	GUI_HMEM hMem;
	char *_acBuffer = NULL;
	DWORD  dwErr = NO_ERROR;
	DWORD  dwSize;

	sprintf(picPath, "%s%s", pPath, sFilename);

	hFile = CreateFile(picPath, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

	if (INVALID_HANDLE_VALUE == hFile)                              //  文件打开失败，返回错误值
		return _acBuffer;

	dwSize = GetFileSize(hFile, NULL);                              //  取文件大小，这里的文件不能太大，否则需要分段读取文件
	
    if (dwSize > 0)
	{
		*pSize = dwSize;

		hMem = GUI_ALLOC_AllocZero(dwSize);
		_acBuffer = GUI_ALLOC_h2p(hMem);

		if (hMem > 0)
        {           
            if( FALSE == ReadFile(hFile, _acBuffer, dwSize, &dwSize, NULL))     //  读取文件失败，记录失败错误值
            {
                GUI_ALLOC_Free(hMem);
                _acBuffer = NULL;
            }
            else
                *pMen = hMem;
        }
		else
			_acBuffer = NULL;
	}
    
	CloseHandle(hFile);

	return (void*)_acBuffer;
}

#else

#define     __BLK_NAME              "/dev/h1-d1-0"
static uint8_t pPath[200], picPath[200];


static void __disk_evt_cb(const char *name, int event, void *p_arg)
{
	/* ?? U? ??????????? U? ?? */
	if (strcmp(name, __BLK_NAME)) {
		return;
	}

	if (event == AW_BD_EVT_INJECT) {
		AW_INFOF(("udisk insert...\r\n"));
	}

	if (event == AW_BD_EVT_EJECT) {
		AW_INFOF(("udisk remove...\r\n"));
	}
}


uint8_t DiskInit(void)
{
	aw_err_t ret;
	bool_t   regist_ok = TRUE;
	int      event_index = 0;


	/*
	 * 添加块设备插拔事件检测，当有U盘插拔时，会回调__disk_evt_cb
	 */
	ret = aw_blk_dev_event_register(__disk_evt_cb, NULL, &event_index);
	if (ret != AW_OK) {
		AW_ERRF(("block event register error: %d!\n", ret));
		regist_ok = FALSE;
	}

	/* 检测U盘块设备是否插入 */
	ret = aw_blk_dev_wait_install(__BLK_NAME, 5000);
	if (ret != AW_OK) {
		AW_ERRF(("wait udisk insert failed, err = %d\r\n", ret));
		goto __task_udisk_fs_end;
	}

	/* 文件系统挂载到 "/u" 结点  */
	ret = aw_mount("/u", __BLK_NAME, "vfat", 0);
	if (ret != AW_OK) {
		AW_ERRF(("/u mount FATFS error: %d!\n", ret));
		goto __task_udisk_fs_end;
	}
	AW_INFOF(("mount OK\n"));


	pPath[0] = 0;
	strcat(pPath, "/u/Sation/Pictures/");

__task_udisk_fs_end:
	return regist_ok;
}



void *GetPictureData(const uint8_t *sFilename, uint32_t *pSize, GUI_HMEM *pMen)
{
	int hFile;
	GUI_HMEM hMem;
	char *_acBuffer = NULL;
	uint32_t  dwSize;
	struct aw_stat  state;

	sprintf(picPath, "%s%s", pPath, sFilename);

	hFile = aw_open(picPath, O_RDONLY, 0777);
	if (hFile < 0) {
		AW_ERRF(("open file error: %d\n", handle));
		return _acBuffer;
	}

	aw_stat(picPath, &state);
	dwSize = state.st_size;

	if (dwSize > 0)
	{
		*pSize = dwSize;

		hMem = GUI_ALLOC_AllocZero(dwSize);
		_acBuffer = GUI_ALLOC_h2p(hMem);

		if (hMem > 0)
        {           
            if( FALSE == aw_read(hFile, _acBuffer, dwSize))     //  读取文件失败，记录失败错误值
            {
                GUI_ALLOC_Free(hMem);
                _acBuffer = NULL;
            }
            else
                *pMen = hMem;
        }
        else
            _acBuffer = NULL;
	}

	aw_close(hFile);

	return (void*)_acBuffer;
}
#endif